﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;

namespace Portal.CMS
{
    public class LawDocumentTypeBLL
    {
        public string ConnectionString
        {
            get
            {
                return Environments.ConnectionString;
            }
        }

        public LawDocumentTypeCls CreateModel(string LawDocumentTypeId)
        {
            string Query = " select TableLawDocumentType.* from TableLawDocumentType where LawDocumentTypeId = '" + LawDocumentTypeId + "'";
            SqlCommand SqlCommand = new SqlCommand(Query, new SqlConnection(ConnectionString));
            SqlCommand.Connection.Open();
            SqlCommand.CommandText = Query;

            SqlDataReader SqlDataReader = SqlCommand.ExecuteReader();
            if (!SqlDataReader.HasRows)
            {
                throw new Exception("Không tìm thấy bản ghi LawDocumentTypeId có mã: " + LawDocumentTypeId);
            }

            SqlDataReader.Read();
            LawDocumentTypeCls tLawDocumentTypeCls = new LawDocumentTypeCls();
            tLawDocumentTypeCls.LawDocumentTypeId = SqlDataReader.GetString(SqlDataReader.GetOrdinal("LawDocumentTypeId"));
            tLawDocumentTypeCls.Subject = SqlDataReader.GetString(SqlDataReader.GetOrdinal("Subject"));

            return tLawDocumentTypeCls;
        }

      

        public DataTable ReadingLawDocumentType(string Keyword)
        {
            DataTable dataTable = new DataTable();
            Keyword = FuncUtilities.CorrectForSearch(Keyword);
            string Query = "select LawDocumentTypeId,Subject from TableLawDocumentType where 1=1 ";
            if (Keyword.Trim().Length > 0)
            {
                Query += " and Subject like N'%" + Keyword + "%'";
            }
            Query += " order by [Subject]";

            SqlDataAdapter da = new SqlDataAdapter(Query, new SqlConnection(ConnectionString));

            da.Fill(dataTable);
            return dataTable;
        }


        public string CreateLawDocumentType(
            SqlTransaction _SqlTransaction,
            string Subject)
        {
            SqlConnection SqlConnection;
            bool HasParentTransaction = false;
            string NewLawDocumentTypeId = System.Guid.NewGuid().ToString();

            if (_SqlTransaction == null)
            {
                SqlConnection = new SqlConnection(ConnectionString);
                SqlConnection.Open();
                _SqlTransaction = SqlConnection.BeginTransaction();
            }
            else
            {
                HasParentTransaction = true;
            }

            string Query = "Insert into TableLawDocumentType(LawDocumentTypeId,Subject) values (@LawDocumentTypeId,@Subject)";
            SqlCommand TempSqlCommand = _SqlTransaction.Connection.CreateCommand();
            TempSqlCommand.Transaction = _SqlTransaction;
            TempSqlCommand.CommandText = Query;
            TempSqlCommand.Parameters.Add("@LawDocumentTypeId", SqlDbType.NVarChar);
            TempSqlCommand.Parameters["@LawDocumentTypeId"].Value = NewLawDocumentTypeId;

            TempSqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            TempSqlCommand.Parameters["@Subject"].Value = Subject;


            try
            {
                TempSqlCommand.ExecuteNonQuery();
                if (HasParentTransaction == false)
                {
                    _SqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasParentTransaction == false)
                {
                    _SqlTransaction.Rollback();
                }
                throw (ex);
            }

            if (HasParentTransaction == false)
            {
                TempSqlCommand.Connection.Close();
            }
            return NewLawDocumentTypeId;
        }



        public void UpdateLawDocumentType(SqlTransaction _SqlTransaction,
            string LawDocumentTypeId,
            string Subject)
        {
            SqlConnection SqlConnection;
            bool HasParentTransaction = false;

            if (_SqlTransaction == null)
            {
                SqlConnection = new SqlConnection(ConnectionString);
                SqlConnection.Open();
                _SqlTransaction = SqlConnection.BeginTransaction();
            }
            else
            {
                HasParentTransaction = true;
            }

            string Query = "Update TableLawDocumentType set Subject=@Subject where LawDocumentTypeId=@LawDocumentTypeId";
            SqlCommand TempSqlCommand = _SqlTransaction.Connection.CreateCommand();
            TempSqlCommand.Transaction = _SqlTransaction;
            TempSqlCommand.CommandText = Query;
            TempSqlCommand.Parameters.Add("@LawDocumentTypeId", SqlDbType.NVarChar);
            TempSqlCommand.Parameters["@LawDocumentTypeId"].Value = LawDocumentTypeId;

            TempSqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            TempSqlCommand.Parameters["@Subject"].Value = Subject;

            try
            {
                TempSqlCommand.ExecuteNonQuery();
                if (HasParentTransaction == false)
                {
                    _SqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasParentTransaction == false)
                {
                    _SqlTransaction.Rollback();
                }
                throw (ex);
            }

            if (HasParentTransaction == false)
            {
                TempSqlCommand.Connection.Close();
            }
        }

        public void DeleteLawDocumentType(SqlTransaction _SqlTransaction,
            string LawDocumentTypeId)
        {
            SqlConnection SqlConnection;
            bool HasParentTransaction = false;
            LawDocumentTypeCls oLawDocumentTypeCls = new LawDocumentTypeBLL().CreateModel(LawDocumentTypeId);
            if (_SqlTransaction == null)
            {
                SqlConnection = new SqlConnection(ConnectionString);
                SqlConnection.Open();
                _SqlTransaction = SqlConnection.BeginTransaction();
            }
            else
            {
                HasParentTransaction = true;
            }


            string Query = "Delete TableLawDocumentType where LawDocumentTypeId=@LawDocumentTypeId";
            SqlCommand TempSqlCommand = _SqlTransaction.Connection.CreateCommand();
            TempSqlCommand.Transaction = _SqlTransaction;
            TempSqlCommand.CommandText = Query;
            TempSqlCommand.Parameters.Add("@LawDocumentTypeId", SqlDbType.NVarChar);
            TempSqlCommand.Parameters["@LawDocumentTypeId"].Value = LawDocumentTypeId;

            try
            {
                TempSqlCommand.ExecuteNonQuery();
                string SubjectDeleted = "";
                if (oLawDocumentTypeCls != null)
                {
                    SubjectDeleted = oLawDocumentTypeCls.Subject;
                }
                Environments.AddDeletedRecordForSync(_SqlTransaction, "TableLawDocumentType","Loại văn bản", LawDocumentTypeId, SubjectDeleted);
                if (HasParentTransaction == false)
                {
                    _SqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasParentTransaction == false)
                {
                    _SqlTransaction.Rollback();
                }
                throw (ex);
            }

            if (HasParentTransaction == false)
            {
                TempSqlCommand.Connection.Close();
            }
        }
    }
}
